package org.canaan.phantom;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 PhantomJs 网页截图
 @Author Canaan
 @Date 2017/12/4 */
public class PhantomScreenShot {
    private final Logger logger = LoggerFactory.getLogger(PhantomScreenShot.class);

    private String   phantomJsPath;   //phantomJs安装路径 如： D:/develop_software/phantomjs/bin/phantomjs.exe
    private Process  process;        //phantomJs进程
    private String   targetUrl;  //url地址 如：http://www.baidu.com
    private String   outputPath;  //为图片输出路径 如：D:/baidu.png
    private String   scriptPath;      //脚本路径

    /**
     PhantomJs 网页截图
     @param phantomJsPath phantomJs安装路径 如： D:/develop_software/phantomjs/bin/phantomjs.exe
     @param targetUrl url地址 如：http://www.baidu.com
     @param outputPath 为图片输出路径 如：D:/baidu.png
     */
    public PhantomScreenShot(String phantomJsPath, String targetUrl, String outputPath) {
        this.phantomJsPath = phantomJsPath;
        this.targetUrl = targetUrl;
        this.outputPath = outputPath;
        if (StringUtils.isBlank(phantomJsPath) || StringUtils.isBlank(targetUrl)) {
            throw new IllegalStateException("phantomJsPath,scriptPath 不能为空");
        }
    }

    /**
     返回图片的输出路径
     @return
     */
    public String execute() {
        if (StringUtils.isBlank(this.scriptPath)) {
            URL url = this.getClass().getClassLoader().getResource("");
            if (url != null) {
                String path = url.getPath();
                this.scriptPath = path + "/js/screenshot.js";
            }
        }

        StringBuilder cmdStr = new StringBuilder();
        cmdStr.append(this.phantomJsPath).append("  ")
                .append(scriptPath).append("  ")
                .append(this.targetUrl).append("  ")
                .append(this.outputPath);

        BufferedReader reader = null;
        try {
            this.process = Runtime.getRuntime().exec(cmdStr.toString());//你的图片输出路径
            InputStream inputStream = this.process.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String result = reader.readLine();
            logger.debug("PhantomJs 截图结果：{}", result);
        } catch (IOException e) {
            logger.error("PhantomJs 截图失败", e);
        } finally {
            close(reader);
        }
        return this.outputPath;
    }

    private void close(BufferedReader bufferedReader) {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                logger.warn("资源关闭失败", e);
            }
        }
        if (this.process != null) {
            this.process.destroy();
            this.process = null;
        }
    }

    public String getScriptPath() {
        return scriptPath;
    }

    public void setScriptPath(String scriptPath) {
        this.scriptPath = scriptPath;
    }


}
